﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StockFinder.Services.Extractors;
using StockFinder.Model;
using StockFinder.Services.Extractors.Yahoo;
using StockFinder.Services.PriceAnalysis;

namespace StockFinder.Forms
{
    public partial class HasJustBrokenOutTestForm : Form
    {
        public HasJustBrokenOutTestForm()
        {
            InitializeComponent();
        }

        private void _RunButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_SymbolNameTextBox.Text))
            {
                MessageBox.Show("Must enter a symbol name");
                return;
            }

            Symbol symbol = new Symbol();
            symbol.Name = _SymbolNameTextBox.Text;

            //1 load prices from yahoo
            IHistoricPricesExtractor _YahooricesExtractor = new YahooHistoricPriceExtractor();
            HistoricPricesExtract result = _YahooricesExtractor.ExtractHistoricPrices(
                DateTime.Now.AddYears(-20), DateTime.Today, symbol.Name);

            if (result.HistoricPricesExtractResult == StockFinder.Model.Enumerations.HistoricPricesExtractResult.SUCCESS)
            {
                //2 order the prices by date
                IOrderedEnumerable<DailyPrice> orderedPrices = result.DailyPrices.OrderByDescending(price => price.PriceDate);

                //3 run the analysis                
                IPriceAnalyser hasJustBrokenOutPriceAnalyser = new JustBrokenOutPriceAnalyser();
                PriceAnalysisResult priceAnalysisResult = hasJustBrokenOutPriceAnalyser.RunAnalysis(orderedPrices);

                _HasJustBrokenOutLabel.Text = string.Empty;
                _BaseDepthLabel.Text = string.Empty;
                _BaseLengthLabel.Text = string.Empty;
                _BaseHighDateLabel.Text = string.Empty;
                _BaseHighPriceLabel.Text = string.Empty;
                _BaseLowDateLabel.Text = string.Empty;
                _BaseLowPriceLabel.Text = string.Empty;
                _LastPriceDateLabel.Text = string.Empty;
                _DaysOfPricesLabel.Text = string.Empty;

                if (priceAnalysisResult.Succeeded)
                {
                    _HasJustBrokenOutLabel.Text = "True";
                    _BaseDepthLabel.Text = priceAnalysisResult.BaseDepth.ToString();
                    _BaseLengthLabel.Text = priceAnalysisResult.BaseLength.ToString();
                    _BaseHighDateLabel.Text = priceAnalysisResult.PreviousHighDate.ToShortDateString();
                    _BaseHighPriceLabel.Text = priceAnalysisResult.PreviousHigh.ToString();
                    _BaseLowDateLabel.Text = priceAnalysisResult.BaselowDate.ToShortDateString();
                    _BaseLowPriceLabel.Text = priceAnalysisResult.BaseLow.ToString();
                    _LastPriceDateLabel.Text = priceAnalysisResult.MostRecentPriceDate.ToShortDateString();
                    _DaysOfPricesLabel.Text = priceAnalysisResult.DaysOfPrices.ToString();
                }
                else
                {
                    _HasJustBrokenOutLabel.Text = "False";
                }
            }
        }
    }
}
